#include<bits/stdc++.h>
using namespace std;

class Solution {
public:
    int getNextN(int n){
        int newN = 0;
        while(n){
            int t = n%10;
            newN += t*t;
            n /= 10;
        }
        return newN;
    }
    bool isHappy(int n) {
        map<int,int> mp;
        while(true){
            n = getNextN(n);
            if(n==1) return true;
            else if(mp[n]) return false;
            else mp[n]=1;
        }
    }
};